package org.example.dyscript.dynamicscript;


import com.greenpineyu.fel.Expression;
import com.greenpineyu.fel.FelEngine;
import com.greenpineyu.fel.FelEngineImpl;
import com.greenpineyu.fel.context.MapContext;

import java.util.HashMap;
import java.util.Map;

public class FelRule implements Rule {

  Expression executeExpression;
  Expression evaluateExpression;

  final static FelEngine fel = new FelEngineImpl();

  String executeScript = "ali_score > 100 "
      + "&& wx_score < 450 "
      + "&& !pd_living_province.contains(\"乌克兰\") "
      + "&& pd_census_province.contains(\"吉尔吉斯斯坦\")";

  String evaluateScript = "age > 18 && vip.contains(\"vip\")";

  public FelRule() {
    Map<String, Object> evaluateParameters = new HashMap<>();

    evaluateParameters.put("age", 0);
    evaluateParameters.put("vip", "string");

    evaluateParameters.put("ali_score", 0);
    evaluateParameters.put("wx_score", 0);

    evaluateParameters.put("pd_living_province", "string");
    evaluateParameters.put("pd_census_province", "string");

    MapContext context = new MapContext(evaluateParameters);

    executeExpression = fel.compile(executeScript, context);
    evaluateExpression = fel.compile(evaluateScript, context);
  }

  @Override
  public boolean execute(Map<String, Object> parameters) {
    return (boolean) fel.eval(executeScript, new MapContext(parameters));
  }

  @Override
  public boolean evaluate(Map<String, Object> parameters) {
    return (boolean) fel.eval(evaluateScript, new MapContext(parameters));
  }

}
